package com.jxd.petMgr.service.impl;

import com.jxd.petMgr.dao.IAccountDao;
import com.jxd.petMgr.dao.IPetDao;
import com.jxd.petMgr.dao.IPetStoreDao;
import com.jxd.petMgr.dao.impl.AccountDaoImpl;
import com.jxd.petMgr.dao.impl.PetDaoImpl;
import com.jxd.petMgr.dao.impl.PetStoreDaoImpl;
import com.jxd.petMgr.model.Account;
import com.jxd.petMgr.service.IAccountService;
import com.jxd.petMgr.service.IPetService;
import com.jxd.petMgr.service.IPetStoreService;
import com.jxd.petMgr.util.PageUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lzh
 * @version 1.0
 * @Description
 * @date 2021/11/5
 */


public class AccountServiceImpl implements IAccountService {
    IAccountDao accountDao = new AccountDaoImpl();
    IPetDao petDao = new PetDaoImpl();
    IPetStoreDao petStoreDao = new PetStoreDaoImpl();
    /**
     * 根据宠物主人id获取所有账单
     *
     * @param id 宠物主人id
     * @return 账单集合
     */
    @Override
    public List<Map<String, Object>> getAccountByPetOwnerId(PageUtil pageUtil,int id) {
        List<Map<String, Object>> accountList = new ArrayList<>();
        List<Account> list = accountDao.getAccountByPetOwnerID(pageUtil,id);
        for (Account account:list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id",account.getId());
            if(account.getDeal_type() == 1){
                map.put("dealType","买入");
                String storeName = petStoreDao.getPetStoreByID(account.getSeller_id()).getStoreName();
                map.put("storeName",storeName);
            } else {
                map.put("dealType","卖出");
                String storeName = petStoreDao.getPetStoreByID(account.getBuyer_id()).getStoreName();
                map.put("storeName",storeName);
            }
            String petName = petDao.getPetById(account.getPet_id()).getName();
            map.put("petName",petName);

            map.put("price",account.getPrice());
            map.put("dealTime",account.getDeal_time());
            accountList.add(map);
        }
        return accountList;
    }

    /**
     * 根据买家id获得账单数目
     *
     * @param id 买家id
     * @return 账单数目
     */
    @Override
    public int getAccountByPetOwnerIDCount(int id) {
        return accountDao.getAccountByPetOwnerIDCount(id);
    }
}
